/**************************************************************

Copyright Astrium Services GEO-Information / Spot Image
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 ***************************************************************/
package com.spotimage.catalog.client.ui;

import com.google.gwt.user.client.Window;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ResizedEvent;
import com.smartgwt.client.widgets.events.ResizedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.spotimage.catalog.client.json.JSONScene;
import com.spotimage.catalog.client.shared.Scene;
import com.spotimage.catalog.client.ui.component.MyWindow;
import com.spotimage.catalog.client.util.SceneTypes;
import com.spotimage.catalog.client.util.Util;

/**
 * Show quicklook of a scene in a window.
 */
public class SceneQuicklookWindow extends MyWindow {
	
	/**
	 * Build UI.
	 * @param scene
	 * @param setTopLeft TODO
	 */
	public SceneQuicklookWindow(Scene scene) {
		super(true);
		setTitle("SPOT" + scene.getId() + " " + SceneTypes.get(scene.getId()));
		setOverflow(Overflow.HIDDEN);
		
		int imgSize = 300;
		
		setAutoCenter(false);
		setTop(Ui.HEADER_HEIGHT + 5);
		setLeft(Window.getClientWidth() - imgSize - 20);
		setWidth(imgSize);

		VLayout layout = new VLayout();
		layout.setWidth100();
		layout.setAlign(Alignment.CENTER);
		
		HLayout hlayout = new HLayout();
		hlayout.setWidth100();
		hlayout.setHeight100();
		hlayout.setAlign(Alignment.CENTER);
		final Img img = new Img(scene.getImageUrl());
		img.setWidth(imgSize);
		img.setHeight(imgSize);
		hlayout.addMember(img);
		layout.addMember(hlayout);

		String dateString = scene.getAcquisitionDateString();
		if (scene instanceof JSONScene) dateString = Util.getDateString(Util.getDateFromISO8601(scene.getAcquisitionDateString()));
		
		String description = "Acquisition date : " + dateString + "<br>" +
								"Cloud cover : " + scene.getCloudCoverPercentage() + "%";
		final Label label = new Label(description);
		label.setHeight(20);
		label.setAlign(Alignment.CENTER);  
		label.setValign(VerticalAlignment.CENTER);  
		layout.addMember(label);
		
		addItem(layout);
		
		show();
		
		addResizedHandler(new ResizedHandler() {
			//@Override
			public void onResized(ResizedEvent event) {
				int newSize = (getWidth() < getHeight())?(getWidth() -10):(getHeight() - (label.getHeight()*3));
				img.setHeight(newSize);
				img.setWidth(newSize);
			}
		});
	}

}
